package com.wine.exam.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wine.common.annotation.RepeatSubmit;
import com.wine.exam.core.controller.BasicController;
import com.wine.exam.dto.QuestionDto;
import com.wine.exam.dto.SubjectDto;
import com.wine.exam.service.IQuestionService;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wine.common.annotation.Log;
import com.wine.common.core.domain.AjaxResult;
import com.wine.common.enums.BusinessType;
import com.wine.exam.domain.Subject;
import com.wine.exam.service.ISubjectService;
import com.wine.common.utils.poi.ExcelUtil;


/**
 * 学科Controller
 *
 * @author wine
 * @date 2022-09-04
 */
@RestController
@Validated
@RequestMapping("/exam/subject")
public class SubjectController extends BasicController {
    @Autowired
    private ISubjectService SubjectService;
    @Autowired
    private IQuestionService QuestionService;

    /**
     * 查询学科列表
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:subject:index')")
    @GetMapping("/list")
    public AjaxResult list(SubjectDto subjectDto) {
        Page<Subject> page = SubjectService.pageQueryList(subjectDto);
        return AjaxResult.success(page);
    }

    /**
     * 导出学科列表
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:subject:export')")
    @Log(title = "学科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SubjectDto subjectDto) {
        Page<Subject> page = SubjectService.pageQueryList(subjectDto);
        List<Subject> records = page.getRecords();
        ExcelUtil<Subject> util = new ExcelUtil<Subject>(Subject.class);
        util.exportExcel(response, records, "学科数据");
    }

    /**
     * 获取学科详细信息
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:subject:query')")
    @GetMapping(value = "/{subjectId}")
    public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId) {
        Subject info = SubjectService.getInfo(subjectId);
        return AjaxResult.success(info);
    }

    /**
     * 新增学科
     */

    @PreAuthorize("@ss.hasPermi('exam:subject:add')")
    @Log(title = "学科", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Subject subject) {
        subject.setCreateBy(getUserId().toString());
        subject.setUpdateBy(getUserId().toString());
        return toAjax(SubjectService.add(subject));
    }

    /**
     * 修改学科
     */
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @PreAuthorize("@ss.hasPermi('exam:subject:edit')")
    @Log(title = "学科", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Subject subject) {
        subject.setUpdateBy(this.getUserId().toString());
        return toAjax(SubjectService.edit(subject));
    }

    /**
     * 修改学科：设置status
     */
    @PreAuthorize("@ss.hasPermi('exam:subject:edit')")
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @Log(title = "学科", businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus/{subjectId}/{status}")
    public AjaxResult editStatus(@PathVariable Long subjectId,
                                 @Pattern(regexp = "[0,1]", message = "问题状态:0=正常,1=停用")
                                 @PathVariable String status) {


        return toAjax(SubjectService.updateStatus(subjectId, status, getUserId().toString()));
    }

    /**
     * 删除学科
     */
    @PreAuthorize("@ss.hasPermi('exam:subject:remove')")
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @Log(title = "学科", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subjectIds}")
    public AjaxResult remove(@PathVariable Long[] subjectIds) {
//        删除是否要再数据库记录更改呢？还是直接日志
        return toAjax(SubjectService.delByIds(subjectIds)
        );
    }

    /**
     * 根据学科查询处有哪些试题
     */
    @PreAuthorize("@ss.hasPermi('exam:subject:index')")
    @RepeatSubmit(interval = 10000 ,message = "不允许重复提交，请稍候再试")
    @Log(title = "学科", businessType = BusinessType.DELETE)
    @GetMapping("/getQuestionBySubjectId")
    public AjaxResult getQuestionBySubjectId(
            @RequestParam("subjectId") Long subjectId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize

    ) {
        QuestionDto questionDto = new QuestionDto();
//        是否考虑分页的问题
        questionDto.setSubjectId(subjectId.toString());
        questionDto.setPageNum(pageNum);
        questionDto.setPageSize(pageSize);
        return success(QuestionService.pageQueryList(questionDto));
    }
}
